package com.tuozixuan;

import org.luaj.vm2.Globals;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.lib.jse.JsePlatform;

public class HelloWorld {

	public static void main(String[] args) {
		String luaStr = "print 'hello, world!'";
		Globals globals = JsePlatform.standardGlobals();
		LuaValue luaValue = globals.load(luaStr);
		luaValue.call();
		
		String luaScriptFile = "hello.lua";
		// 加载脚本文件并编译
		globals.loadfile(luaScriptFile).call();
		// 获取无参函数hello
		LuaValue helloFunction = globals.get(LuaValue.valueOf("hello"));
		// 执行hello函数
		helloFunction.call();
		
		// 获取带参函数test
		LuaValue testFunction = globals.get(LuaValue.valueOf("test"));
		// 执行test函数并传入String类型的参数
		String data = testFunction.call(LuaValue.valueOf("I'm from Java!")).toString();
		System.out.println("data return from lua: " + data);
	}

}
